package leetcode.code0764;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	int n;

	public int orderOfLargestPlusSign(int n, int[][] mines) {
		this.n = n;
		int[][] grid = new int[n][n];
		for (int i = 0; i < mines.length; i++) {
			int[] mine = mines[i];
			grid[mine[0]][mine[1]] = 1;
		}
		int[][] rcl = this.rcl(grid);
		int[][] rcr = this.rcr(grid);
		int[][] cru = this.cru(grid);
		int[][] crd = this.crd(grid);
		int ans = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == 0) {
					int cur = Math.min(Math.min(rcl[i][j], rcr[i][j]), Math.min(cru[i][j], crd[i][j]));
					ans = Math.max(ans, cur);
				}
			}
		}
		return ans;
	}

	// 行列从左向右，连续的0
	public int[][] rcl(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		int[][] ans = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				ans[i][j] = grid[i][j] == 0 ? j == 0 ? 1 : ans[i][j - 1] + 1 : 0;
			}
		}
		return ans;
	}

	// 行列从右向左，连续的0
	public int[][] rcr(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		int[][] ans = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = n - 1; j >= 0; j--) {
				ans[i][j] = grid[i][j] == 0 ? j == n - 1 ? 1 : ans[i][j + 1] + 1 : 0;
			}
		}
		return ans;
	}

	// 列行从上向下，连续的0
	public int[][] cru(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		int[][] ans = new int[m][n];
		for (int j = 0; j < n; j++) {
			for (int i = 0; i < m; i++) {
				ans[i][j] = grid[i][j] == 0 ? i == 0 ? 1 : ans[i - 1][j] + 1 : 0;
			}
		}
		return ans;
	}

	// 列行从下向上，连续的0
	public int[][] crd(int[][] grid) {
		int m = grid.length, n = grid[0].length;
		int[][] ans = new int[m][n];
		for (int j = 0; j < n; j++) {
			for (int i = m - 1; i >= 0; i--) {
				ans[i][j] = grid[i][j] == 0 ? i == m - 1 ? 1 : ans[i + 1][j] + 1 : 0;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(2, this.orderOfLargestPlusSign(5, H.str2array1("[[4,2]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
